package TimesheetManagemen;

import java.sql.Array;
import java.sql.SQLException;
import java.util.ArrayList;

public class App {
	public void addEmployee(Employee emp) throws ClassNotFoundException, SQLException{
		EmployeeDAO dao = new EmployeeDAO();
		ArrayList<Employee> list = dao.getEmployee();
	    int a=0;
		for(int i=0;i<list.size();i++)
		{
			int c=list.get(i).getId();
			if(c==emp.getId())
			{a=1;
			break;}
		}
		 if( a==1){
				System.out.println("Id exist");	
		    }   
		     else if(dao.addEmployee(emp)==1) {
		    	 
		    	 
		     	System.out.println("Add Successfull");
		}
		     else if(dao.addEmployee(emp)==0 ){
					System.out.println("Not Add Successfull");
			     }}
	public void updateEmployee(Employee emp) throws ClassNotFoundException, SQLException{
		EmployeeDAO dao=new EmployeeDAO();
		ArrayList<Employee> list=dao.getEmployee();
		int a=0;
		for(int i=0;i<list.size();i++){
			int c=list.get(i).getId();
		if(c==emp.getId()){
			a=1;break;
		}}
		if(a==1){	 
			    
		int kq = dao.updateEmployee(emp);
	    if(kq!=0){
	    	System.out.print("Update Successful");
		}else{
			System.out.println("Not Update Successfull");
		}}
		else{
			System.out.println("Not Found Id ");
	    }}
	public void addProject(Project pj){
		ProjectDAO pjo=new ProjectDAO();
		 ArrayList<Project> list = pjo.getProject();
		    int a=0;
			int b=0;
			for(int i=0;i<list.size();i++)
			{
				int c=list.get(i).getId();
				String d=list.get(i).getNameProject();
				if(c==pj.getId())
				{a=1;
				break;}
				if(pj.getNameProject().equals(d)){
					b=1;
					break;
				}
			}
			 if( a==1){
			    	
					System.out.println("id exist");	
			    }
			 else if(b==1){
				 System.out.println("Name exist");
			 }
			     else if(pjo.addProject(pj)==1) {
			     	System.out.println("Add Successfull");
			    	
			}
			     else if(pjo.addProject(pj)==0 ){
						System.out.println("Not Add Successfull");
				     }   
	}
	public void updateProject(Project pj){
		ProjectDAO pjo=new ProjectDAO();
		 ArrayList<Project> list = pjo.getProject();
			int a=0;
			for(int i=0;i<list.size();i++){
				int c=list.get(i).getId();		
			if(c==pj.getId()){
				a=1;break;
			}}
			if(a==1){	 
			int kq = pjo.updateProject(pj);
		    if(kq!=0){
		    	System.out.print("Update Successful");
			}else{
				System.out.println("Not Update Successfull");
			}}
			else{
				System.out.println("Not Found Id");
		    }}
	public void addTimesheet(Timesheet ts1) throws ClassNotFoundException, SQLException{
		TimesheetDAO ts = new TimesheetDAO();
		ProjectDAO pjo=new ProjectDAO();
		EmployeeDAO dao = new EmployeeDAO();
		 ArrayList<Timesheet> list = ts.getTimesheet();
		 ArrayList<Employee> listemp=dao.getEmployee();
		 ArrayList<Project> listpj=pjo.getProject();
		    int a=0;
			int b=0;
			int e=0;
			for(int i=0;i<list.size();i++){
				for(int j=0;j<listemp.size();j++){
					for(int m=0;m<listpj.size();m++)
			{
				int c=list.get(i).getId();
				if(c==ts1.getId())
				{a=1;
				break;}
				int g=listemp.get(j).getId();
				if(g==ts1.getEmployeeid()){
					b=1;break;
				}
				int h=listpj.get(m).getId();
				if(h==ts1.getProjectid()){
					e=1;break;
				}
			}}}
			 if( a==1){
					System.out.println("Id exist ");	
			    }
			 else if(b==0){
				 System.out.println("Not Found Employeeid ");
			 }
			 else if(e==0){
				 System.out.println("Not Found Projectid");
			 }
			     else if(ts.addTimesheet(ts1)==1) {
			     	System.out.println("Add Successfull");   	
			}
			     else if(ts.addTimesheet(ts1)==0 ){
						System.out.println("Not Add Successfull");
				     }		  
	}
	public void updateTimesheet(Timesheet ts1) throws ClassNotFoundException, SQLException{
		TimesheetDAO ts = new TimesheetDAO();
		ProjectDAO pjo=new ProjectDAO();
		EmployeeDAO dao = new EmployeeDAO();
		 ArrayList<Timesheet> list = ts.getTimesheet();
		 ArrayList<Employee> listemp=dao.getEmployee();
		 ArrayList<Project> listpj=pjo.getProject();
		 int a=0;
			int b=0;
			int d=0;
			for(int i=0;i<list.size();i++){
				for(int j=0;j<listemp.size();j++){
					for(int h=0;h<listpj.size();h++){
						int c=list.get(i).getId();
						if(c==ts1.getId()){
							a=1;break;	}
						int e=listemp.get(j).getId();
						if(e==ts1.getEmployeeid()){
							b=1;break;}
						int f=listpj.get(h).getId();
						if(f==ts1.getProjectid()){
							d=1;break;
						}}}	}
			if(a==0){
				System.out.println("Not Found Id");
			}
			else if(b==0){
				System.out.println("Not Found Employeeid");
			}
			else if(d==0){
				System.out.println("Not Found Projectid");
			}
			else if(ts.updateTimesheet(ts1)==1){
				System.out.println("Update Successfull");
			}}
}
